home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C13 / MallocClass.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  870 b   |  37 lines

  1. //: C13:MallocClass.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Malloc with class objects
  7. // What you'd have to do if not for "new"
  8. #include "../require.h"
  9. #include <cstdlib> // Malloc() & free()
  10. #include <cstring> // Memset()
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. class Obj {
  15.   int i, j, k;
  16.   static const int sz = 100;
  17.   char buf[sz];
  18. public:
  19.   void initialize() { // Can't use constructor
  20.     cout << "initializing Obj" << endl;
  21.     i = j = k = 0;
  22.     memset(buf, 0, sz);
  23.   }
  24.   void destroy() { // Can't use destructor
  25.     cout << "destroying Obj" << endl;
  26.   }
  27. };
  28.  
  29. int main() {
  30.   Obj* obj = (Obj*)malloc(sizeof(Obj));
  31.   require(obj != 0);
  32.   obj->initialize();
  33.   // ... sometime later:
  34.   obj->destroy();
  35.   free(obj);
  36. } ///:~
  37.